package com.gzhryc.shared_device.oem.micro.api.controllers.xcx.v1.member;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.ClassTools;
import com.gzhryc.common.api.DefaultAPIResponse;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.business.dao.db.CouponActivity;
import com.gzhryc.shared_device.oem.code.business.dao.enums.CouponActivityEnum;
import com.gzhryc.shared_device.oem.code.business.services.CouponActivityService;
import com.gzhryc.shared_device.oem.code.member.LoginMember;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberCoupon;
import com.gzhryc.shared_device.oem.code.member.services.MemberCouponService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberCouponSearch;
import com.gzhryc.shared_device.oem.micro.api.MicroConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.CouponActivityReceiveRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.CouponActivityScanInfoRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.CouponActivityScanInfoResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberCouponListRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberCouponListResponse;

public class MemberCouponController {

    static Logger log = Logger.getLogger(MemberCouponController.class);

    @Route(value = { "/xcx/v1/member/coupon/list.data" }, method = "POST")
    public MemberCouponListResponse list(HttpServletRequest request) {
        MemberCouponListResponse apiResp = new MemberCouponListResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
        MemberCouponListRequest apiReq = WebUtils.getJsonRequest(request, MemberCouponListRequest.class);
        LoginMember member = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
        if (apiReq != null && member != null) {
            Integer pageNo = apiReq.getBody().getPageNo() == null ? 1 : apiReq.getBody().getPageNo();
            MemberCouponSearch search = new MemberCouponSearch();
            search.setMemberId(member.getId());
            search.setState(apiReq.getBody().getState());
            List<MemberCoupon> couponList = MemberCouponService.self().findBySearch(search, pageNo, MicroConstants.PAGE_LIMIT);
            if (couponList != null && couponList.size() > 0) {
                apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                apiResp.getBody().setItems(new ArrayList<>());
                for (MemberCoupon coupon : couponList) {
                    MemberCouponListResponse.Item item = new MemberCouponListResponse.Item();
                    item.setId(coupon.getId());
                    item.setTitle(coupon.getTitle());
                    item.setUseRange(coupon.getUseRange());
                    item.setPlaceId(coupon.getPlaceId());
                    item.setNote(coupon.getNote());
                    item.setState(coupon.getState());
                    item.setStateName(coupon.getStateName());
                    item.setExpirationDate(coupon.getExpirationDate());
                    apiResp.getBody().getItems().add(item);
                }
            }
        }
        return apiResp;
    }

    @Route(value = { "/xcx/v1/coupon_activity/scan/get.do" }, method = "POST")
    public CouponActivityScanInfoResponse couponScanGet(HttpServletRequest request) {
        CouponActivityScanInfoResponse apiResp = new CouponActivityScanInfoResponse(ClientAPICodeConstants.NULL_DATA, "未找到活动信息");
        try {
            CouponActivityScanInfoRequest apiReq = WebUtils.getJsonRequest(request, CouponActivityScanInfoRequest.class);
            if (apiReq != null && apiReq.getBody().getActivityId() != null) {
                CouponActivity couponActivity = CouponActivityService.self().getJoinById(apiReq.getBody().getActivityId());
                if(couponActivity != null){
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                    CouponActivityScanInfoResponse.Body body = ClassTools.copy(new CouponActivityScanInfoResponse.Body(),couponActivity);
                    body.setCouponTypeName(couponActivity.getCouponTypeName());
                    body.setCouponMoneyStr(couponActivity.getCouponMoneyStr());
                    body.setUseRangeName(couponActivity.getUseRangeName());
                    body.setUseLimitName(couponActivity.getUseLimitName());
                    body.setBusinessTypeName(couponActivity.getBusinessTypeName());
                    body.setStateName(couponActivity.getStateName());
                    apiResp.setBody(body);
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return apiResp;
    }

    @Route(value = { "/xcx/v1/coupon_activity/receive.do" }, method = "POST")
    public DefaultAPIResponse receiveCoupon(HttpServletRequest request) {
        DefaultAPIResponse apiResp = new DefaultAPIResponse(ClientAPICodeConstants.EXECUTE_ERROR, "领取优惠券失败");
        try {
            CouponActivityReceiveRequest apiReq = WebUtils.getJsonRequest(request, CouponActivityReceiveRequest.class);
            if (apiReq != null && apiReq.getBody().getActivityId() != null) {
                CouponActivity couponActivity = CouponActivityService.self().getJoinById(apiReq.getBody().getActivityId());
                if(couponActivity != null && CouponActivityEnum.State.Enable.index().equals(couponActivity.getState())){
                    LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
                    OperateInfo operateInfo = new OperateInfo();
                    operateInfo.setProgramType(MicroConstants.PROGRAM_TYPE);
                    operateInfo.setProgramTypeName(MicroConstants.PROGRAM_TYPE_NAME);
                    operateInfo.setOperatorType(EOperatorType.Member.index());
                    operateInfo.setOperatorTypeName(EOperatorType.Member.note());
                    operateInfo.setOperator(loginMember.getFullName());
                    if(CouponActivityService.self().memberReceive(couponActivity,loginMember.getId()
                            ,loginMember.getNickname(),apiReq.getBody().getPlaceId(),operateInfo)){
                        apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                    }
                }else{
                    apiResp.setMsg("优惠活动不存在或已结束");
                }
            }
        }catch (LogicException e){
            apiResp.setMsg(e.getMessage());
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return apiResp;
    }
}
